import CONST from '@/const'
import * as api from '@/api/common'

const dict = {
  state: {
    common: []
  },
  mutations: {
    SET_DICT: (state, dict) => {
      state.common = dict
    }
  },
  actions: {
    // 获取数据字典
    GetDict({ commit }) {
      return new Promise((resolve, reject) => {
        // TODO 添加整个项目所需频次多的字典码
        const codes = [
          CONST.DICT.COMMON.COMM_UNIT,
          CONST.DICT.COMMON.COMM_SEX,
          CONST.DICT.COMMON.COMM_CURRENCY,
          CONST.DICT.COMMON.COMM_COUNTRY,
          CONST.DICT.AUTHORITY.AUTHORITY_ELEMENT,
          CONST.DICT.AUTHORITY.AUTHORITY_MENU,
          CONST.DICT.PRODUCT.MODULE_PARAM_TYPE
        ]
        const params = {
          codes: codes.join(',')
        };
        api.getDict(params).then(response => {
          const data = response.data
          console.log('dict', data)
          commit('SET_DICT', data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default dict
